package org.nado.web;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.nado.bbs.BBSBoardVO;
import org.nado.bbs.BBSService;
import org.nado.bbs.ReplyVO;
import org.nado.bbs.SearchVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller("BBSController")
@RequestMapping(value = "/bbs/*")
@SessionAttributes("searchList")
public class BBSController {

	private static final Logger logger = LoggerFactory
			.getLogger(BBSController.class);

	@Inject
	BBSService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView getList(@RequestParam(value = "page_no") int page_no) throws Exception {
		logger.info("getList.........");

		ModelAndView mav = new ModelAndView();
		List<BBSBoardVO> list = service.list(page_no);

		if (list != null) {
			mav.addObject("BBSList", list);
		} else {
			mav.addObject("BBSList", null);
		}
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/list");
		return mav;
	}

	@RequestMapping(value = "/read", method = RequestMethod.GET)
	public ModelAndView getContent(@RequestParam("bbs_no") int bbs_no,
			@RequestParam("page_no") String page_no)
			throws Exception {
		ModelAndView mav = new ModelAndView();
		BBSBoardVO vo = service.get(bbs_no);
		mav.addObject("content", vo);
		logger.info("content: " + vo);
		
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/read");
		return mav;
	}

	@RequestMapping(value = "/write", method = RequestMethod.POST)
	public ModelAndView writeContent(
			@RequestParam(value = "bbs_no", required = false, defaultValue = "0") int bbs_no,
			@RequestParam(value = "page_no", required = false, defaultValue = "0") int page_no) 
			throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.addObject("bbs_no", bbs_no);
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/write");
		return mav;
	}

	@RequestMapping(value = "/writeResult")
	public ModelAndView writeResult(BBSBoardVO vo,
			@RequestParam(value = "bbs_no", required = false, defaultValue = "0") int bbs_no,
			@RequestParam(value = "page_no", required = false, defaultValue = "1") int page_no)
			throws Exception {
		ModelAndView mav = new ModelAndView();
		
		if (bbs_no == 0) {
			logger.info("bbs_no: " + bbs_no);
			vo = service.registNew(vo);
		} else {
			logger.info("bbs_no: " + bbs_no);
			BBSBoardVO content = service.get(bbs_no);
			logger.info("content_before: " + content);
			content.setBbs_title(vo.getBbs_title());
			content.setBbs_writer(vo.getBbs_writer());
			content.setBbs_content(vo.getBbs_content());
			logger.info("content_after: " + content);
			vo = service.registUp(content);
		}
		logger.info("vo: " + vo);
		mav.addObject("content", vo);
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/writeResult");
		return mav;
	}

	@RequestMapping(value = "/remove")
	public ModelAndView removeContent(@RequestParam("bbs_no") int bbs_no,
			@RequestParam("page_no") int page_no) throws Exception {
		ModelAndView mav = new ModelAndView();
		int bbs_pno = 0;
		BBSBoardVO content = service.get(bbs_no);
		if (content != null) {
			bbs_pno = content.getBbs_pno();
		}
		logger.info("bbs_no: " + bbs_no);
		logger.info("bbs_pno: " + bbs_pno);
		service.remove(bbs_no, bbs_pno);

		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/removeResult");
		return mav;
	}

		@RequestMapping(value="/modify")
	public ModelAndView modifyContent(@RequestParam("bbs_no") int bbs_no,
			@RequestParam("page_no") int page_no) throws Exception{
		ModelAndView mav = new ModelAndView();
		BBSBoardVO content = service.get(bbs_no);
		mav.addObject("content", content);
		mav.addObject("bbs_no", bbs_no);
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/modify");
		return mav;
	}

	@RequestMapping(value="/modifyResult")
	public ModelAndView modifyResult(BBSBoardVO vo,
		@RequestParam("bbs_no") int bbs_no, @RequestParam("page_no") int page_no)
		throws Exception{
		ModelAndView mav = new ModelAndView();
		vo.setBbs_no(bbs_no);
		service.modify(vo);
		vo = service.get(bbs_no);
		mav.addObject("content", vo);
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/modifyResult");
		return mav;
	}
	
//	@RequestMapping(value = "/search")
//	public ModelAndView search(SearchVO svo, @RequestParam("page_no") int page_no, Model model) 
//			throws Exception {
//
//		ModelAndView mav = new ModelAndView();
//		logger.info("type: "+svo.getType());
//		logger.info("keyword: "+svo.getKeyword());
//		logger.info("page_no: "+page_no);
//
//		List<BBSBoardVO> list = service.search(svo.getType(), svo.getKeyword(), page_no);
//
//		logger.info("list---------"	+ list.toString());
//
//		model.addAttribute("searchList", list);
////		mav.addObject("searchList", list);
//		mav.addObject("page_no", page_no);
//		mav.setViewName("bbs/searchList");
//		return mav;
//	}
	
	@RequestMapping(value = "/searchList", method = RequestMethod.GET)
	public ModelAndView getSearchList(@RequestParam(value = "page_no") int page_no) throws Exception {
		logger.info("getList.........");

		ModelAndView mav = new ModelAndView();
		List<BBSBoardVO> list = service.list(page_no);

		if (list != null) {
			mav.addObject("BBSList", list);
		} else {
			mav.addObject("BBSList", null);
		}
		mav.addObject("page_no", page_no);
		mav.setViewName("bbs/searchList");
		return mav;
	}

	@RequestMapping(value = "/getReply/{bbs_rep_no}.json", headers = "Accept=application/json", produces = { MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody List<ReplyVO> getReply(@PathVariable("bbs_rep_no") Integer bbs_rep_no)
			throws Exception {

		logger.info("get reply :" + bbs_rep_no);
		List<ReplyVO> replyList = service.replyList(bbs_rep_no);

		logger.info("get replyList" + replyList.toString());
		return replyList;

	}

	@RequestMapping(value = "/addReply")
	public String addReply(ReplyVO vo) throws Exception {
		logger.info(vo.toString());
		service.replyResist(vo);

		return "bbs/read";
	}

	@RequestMapping(value = "/removeReply")
	public String removeReply(ReplyVO vo)	 throws Exception {

		logger.info("remove" +vo.toString());
		service.replyDelete(vo);
		return "bbs/read";

	}

}